package com.sunlands.zlcx.usercenter.repository;

import com.sunlands.zlcx.usercenter.domain.SmsMobileIdentifyCode;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @author anjl
 * @date 1:30 PM 2019/4/15
 * description:
 */
@Repository
public interface SmsMobileIdentifyCodeRepository extends JpaRepository<SmsMobileIdentifyCode, Integer> {

    @Query(value = "SELECT * FROM tz_mobile_identify_code c WHERE c.phone=?1 AND c.status=?2 AND c.identify_code=?3 AND c.expire_time > ?4", nativeQuery = true)
    List<SmsMobileIdentifyCode> findByPhoneNoAndStateAndIdentifyCodeAndExpireTimeAfter(String phone, Integer status, String identifyCode, String expireTime);

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update tz_mobile_identify_code c set status=?2 where c.id =?1")
    int updateState(Long id, Integer state);
}
